package com.huilan.etl.demo;

import com.huilan.etl.util.BodyType;
import com.huilan.etl.util.DateUtil;
import com.huilan.etl.util.HeadType;
import com.huilan.etl.util.MsgType;
import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;
import com.taocares.sdxp.SdxpConstants;
import com.taocares.sdxp.client.SDXPClient;
import com.taocares.sdxp.client.SDXPClientFactory;
import com.taocares.sdxp.model.LoginResult;
import com.taocares.sdxp.model.MessageResult;
import com.taocares.sdxp.model.SendResult;
import com.taocares.sdxp.util.JAXBUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ghy
 * @date 2019-01-03 13:10
 **/
@Slf4j
public class Demo {
    private static String host = "192.168.163.103";
    private static Integer port = 7801;
    private static String username = "ss";
    private static String password = "123456";

    private  static  SDXPClient sdxpClient = SDXPClientFactory.createWSClient(host, port);


    @Test
    public  void test() {
        String host = "192.168.163.103";
        Integer port = 7801;
        String username = "ss";
        String password = "123456";
        SDXPClient client = SDXPClientFactory.createWSClient(host, port);

        while(true) {
            MessageResult result = client.receiveMessage();
            if (!result.isSuccess()) {
                if (result.getCode() != 801 && result.getCode() != 501) {
                    throw new RuntimeException(result.getGuid());
                }

                client.login(username, password);
            } else {
                System.out.println(result);
            }
        }
    }


    @Test
    public  void test1() throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<Msg>" +
                "    <Head>" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALIE</Svc_ServiceCode>" +
                "        <Svc_Version>1.0</Svc_Version>" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>" +
                "        <Svc_Sender>NAOMS</Svc_Sender>" +
                "        <Svc_Receiver_Org></Svc_Receiver_Org>" +
                "        <Svc_Receiver></Svc_Receiver>" +
                "        <Svc_SerialNumber></Svc_SerialNumber>" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>" +
                "</Head>" +
                "<Body>" +
                "        <Airline>" +
                "            <Id>16692</Id>" +
                "            <Iata>CA</Iata>" +
                "            <Icao>CCA</Icao>" +
                "            <Region>D</Region>" +
                "            <Cn>国航</Cn>" +
                "            <En>AIR CHINA</En>" +
                "            <CnFull>中国国际航空公司</CnFull>" +
                "            <EnFull>AIR CHINA</EnFull>" +
                "            <Union>星空联盟</Union>" +
                "        </Airline>   " +
                "</Body>" +
                "</Msg>";



        String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<Msg>" +
                "    <Head>" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALDL</Svc_ServiceCode>" +
                "        <Svc_Version>1.0</Svc_Version>" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>" +
                "        <Svc_Sender>NAOMS</Svc_Sender>" +
                "        <Svc_Receiver_Org>ADXP</Svc_Receiver_Org>" +
                "        <Svc_Receiver>FIDS</Svc_Receiver>" +
                "        <Svc_SerialNumber></Svc_SerialNumber>" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>" +
                "    </Head>" +
                "    <Body>" +
                "        <AirlineList>" +
                "            <Airline>" +
                "                <Id>1669211</Id>" +
                "                <Iata>CA</Iata>" +
                "                <Icao>CCA</Icao>" +
                "                <Region>D</Region>" +
                "                <Cn>国航</Cn>" +
                "                <En>AIR CHINA</En>" +
                "                <CnFull>中国国际航空公司</CnFull>" +
                "                <EnFull>AIR CHINA</EnFull>" +
                "                <Union>星空联盟</Union>" +
                "            </Airline>" +
                "            <Airline>" +
                "                <Id>1669311</Id>" +
                "                <Iata>CA</Iata>" +
                "                <Icao>CCA</Icao>" +
                "                <Region>D</Region>" +
                "                <Cn>国航</Cn>" +
                "                <En>AIR CHINA</En>" +
                "                <CnFull>中国国际航空公司</CnFull>" +
                "                <EnFull>AIR CHINA</EnFull>" +
                "                <Union>星空联盟</Union>" +
                "            </Airline>" +
                "        </AirlineList>" +
                "    </Body>" +
                "</Msg>";
        MsgType msgType = JAXBUtil.unmarshalIgnoreNameSpace(MsgType.class, xml1);
        ElementNSImpl elementNS = (ElementNSImpl)msgType.getBody();
        System.out.println(elementNS.getChildNodes());
    }


    @Test
    public void send() {
        String xml = "<Msg>" +
                "    <Head>" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_RQAL</Svc_ServiceCode>" +
                "        <Svc_Version>1.0</Svc_Version>" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>" +
                "        <Svc_Sender>SS</Svc_Sender>" +
                "        <Svc_Receiver_Org>ADXP</Svc_Receiver_Org>" +
                "        <Svc_Receiver>NAOMS</Svc_Receiver>" +
                "        <Svc_SerialNumber></Svc_SerialNumber>" +
                "        <Svc_SessionId>20190103153725403</Svc_SessionId>" +
                "        <Svc_SendTimeStamp>20190103153725403</Svc_SendTimeStamp>" +
                "    </Head>" +
                "    <Body>" +
                "        <Request>" +
                "        </Request>" +
                "    </Body>" +
                "</Msg>";

        List<String> messageList = new ArrayList<>();
        messageList.add(xml);
        sendMessage(messageList);
        while(true) {
            MessageResult result = sdxpClient.receiveMessage();
            if (!result.isSuccess()) {
                if (result.getCode() != 801 && result.getCode() != 501) {
                    throw new RuntimeException(result.getGuid());
                }
                sdxpClient.login(username, password);
            } else {
                System.out.println(result);
            }
        }
    }

    private static void sendMessage(List<String> messageList){
        SendResult sendResult = sdxpClient.sendMessage(messageList);

        if(!sendResult.isSuccess()&&sendResult.getCode()== SdxpConstants.RC_CLIENT_NOT_LOGGED_IN||sendResult.getCode()==SdxpConstants.RC_TOKEN_EXPIRED){
            if(login()){
                sendResult = sdxpClient.sendMessage(messageList);
            }
        }

        if(sendResult.isSuccess()){
            log.info("用户：{}成功发送消息{}条",username,messageList.size());
        }else{
            log.error("用户：{}发送消息失败",username,sendResult.getMessage());
        }
    }

    private static boolean login(){
        LoginResult loginResult = sdxpClient.login(username,password);
        if(loginResult.isSuccess()){
            log.info("用户：{}登录平台成功",username);
        }else{
            log.error("用户：{}登录平台失败",username,loginResult.getMessage());
        }
        return loginResult.isSuccess();
    }


    public static List<String> receiveMessage(){
        MessageResult messageResult = sdxpClient.receiveMessage();
        if(!messageResult.isSuccess()&&messageResult.getCode()==SdxpConstants.RC_CLIENT_NOT_LOGGED_IN||messageResult.getCode()==SdxpConstants.RC_TOKEN_EXPIRED){
            if(login()){
                messageResult = sdxpClient.receiveMessage();
            }
        }
        if(messageResult.isSuccess()){
            log.info("用户：{}成功接收消息{}条",username,messageResult.getMessageStringList().size());
        }else{
            log.error("用户：{}接收消息失败，{}",username,messageResult.getGuid());
        }
        return messageResult.getMessageStringList();
    }

    @Test
    public void ttt(){
        String  time = DateUtil.formatDate2Str(new Date(),DateUtil.YYYYMMDDHHMMSS);

        String xml = "<Msg>" +
                "    <Head>" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_RQAL</Svc_ServiceCode>" +
                "        <Svc_Version>1.0</Svc_Version>" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>" +
                "        <Svc_Sender>SS</Svc_Sender>" +
                "        <Svc_Receiver_Org>ADXP</Svc_Receiver_Org>" +
                "        <Svc_Receiver>SS</Svc_Receiver>" +
                "        <Svc_SerialNumber></Svc_SerialNumber>" +
                "        <Svc_SessionId>20190103153725403</Svc_SessionId>" +
                "        <Svc_SendTimeStamp>20190103153725403</Svc_SendTimeStamp>" +
                "    </Head>" +
                "    <Body>" +
                "        <Request>" +
                "        </Request>" +
                "    </Body>" +
                "</Msg>";


        MsgType msgType = new MsgType();
        HeadType headType = new HeadType();
        headType.setSvcServiceCode("ADXP_NAOMS_O_BASE_RQAL");
        headType.setSvcVersion("1.0");
        headType.setSvcSenderOrg("ADXP");
        headType.setSvcSender("SS");
        headType.setSvcReceiverOrg("ADXP");
        headType.setSvcReceiver("SS");
        headType.setSvcSerialNumber("");
        headType.setSvcSessionId(time);
        headType.setSvcSendTimeStamp(time);
        msgType.setHead(headType);
        BodyType bodyType = new BodyType();
        msgType.setBody(bodyType);
        System.out.println(JAXBUtil.convertToXml(msgType));
    }
}